package com.ncu.web.service;

import com.ncu.web.dao.mapper.ContactsMapper;
import com.ncu.web.dao.mapper.UserMapper;
import com.ncu.web.entry.Contacts;
import com.ncu.web.entry.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Component
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ContactsMapper contactsMapper;

    public Student selectStudentById(String id){
        return userMapper.selectById(id);
    }

    public Student selectStudentByPhone(long phone){
        return userMapper.selectByPhone(phone);
    }

    public Student selectStudentByEmail(String email){
        return userMapper.selectByEmail(email);
    }

    public  Contacts insertContactsInformation( int class_num, int entry_year, int graduate_year, String employers, String city, long phone, String email, int majority){
        Contacts contacts = new Contacts();
        contacts.setCity(city);
        contacts.setEmail(email);
        contacts.setPhone(phone);
        contacts.setEmployers(employers);
        contacts.setClassNum(class_num);
        contacts.setMajority(majority);
        contacts.setEntryYear(entry_year);
        contacts.setGraduateYear(graduate_year);

        contactsMapper.insert(contacts);
        return contacts;
    }

    public int insertStudentInformation(String id, String password, Contacts contacts, String name){
        Student student = new Student();
        student.setId(id);
        student.setPassword(password);
        student.setContacts(contacts.getId());
        student.setLoginTimes(0);
        student.setName(name);
        student.setPermission((short) 0);

        return userMapper.insert(student);
    }

    public int updateLoginTimes(String id, int loginTimes) {
        return userMapper.updateLoginTimesInt(id, loginTimes);
    }

    public int updateLastLoginTime(String id, Timestamp lastLoginTime) {
        return userMapper.updateLastLoginTime(id, lastLoginTime);
    }

    public int updateStudent(Student student) {
        return userMapper.updateStudent(student.getPassword(), student.getName(), student.getId());
    }

    public List<Map<String, Object>> countByMajority() {
        return contactsMapper.countByMajority();
    }

    public List<Map<String, Object>> countByCity() {
        return contactsMapper.countByCity();
    }

}
